/**
 * name:        登录
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

$(function(){ 
    // 是否记住密码; 
    function cook(){ 
        var cookie_name=$('#phone').val(); 
        var cookie_pass=$('#password').val(); 
        if($('#checkbox_m').is(':checked')){ 
            $.cookie('username',cookie_name,{expires:7, path:'/'}); 
            $.cookie('password',cookie_pass,{expires:7, path:'/'}); 
        }else{ 
            $.cookie('username','',{expires:-1, path:'/'}); 
            $.cookie('password','',{expires:-1, path:'/'}); 
        } 
    } 
    function ajax_form(){ 
        function phone(){ 
            var phone = $('#phone').val();  // 获取输入框的手机号码 
            // 验证手机号码; 
            if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){ 
                $('#phone').addClass('error_bor'); 
                $('.error_bor').parents('li').find('.error').show(); 
                return false; 
            }else{ 
                $('#phone').removeClass('error_bor'); 
                $('#phone').parents('li').find('.error').hide(); 
            }; 
            // 验证密码 
            if($('#password').val().length<6){ 
                $('#password').addClass('error_bor'); 
                $('.error_bor').parents('li').find('.error').show(); 
                return false; 
            }else{ 
                $('#password').removeClass('error_bor'); 
                $('#password').parents('li').find('.error').hide(); 
            }; 
        }; 
        // 登录 
        $('#login_sub').on('click',function(){ 
            phone(); // 提交验证 
            if($('.login_bg ul li input').hasClass('error_bor')){ 
                return false; 
            }else{ 
                $.ajax({ 
                    type:'post', 
                    url:new_url+'shop/login/login', 
                    dataType:'json', 
                    data:$(".register_tab").serialize(), 
                    success:function(e){ 
                        var mycars = new Array(); 
                        mycars[0] = e.data.name; 
                        mycars[1] = e.data.nickName; 
                        mycars[2] = e.data.phone; 
                        mycars[3] = e.data.avatar; 
                        mycars[4] = e.data.truename; 
                        mycars[5] = e.data.memberId; 
                        mycars[6] = e.data.token; 
                        mycars[7] = 
                        // 设置用户cookies 
                        $.cookie('user',mycars,{path:'/'}); 
                        // 设置用户购物车cookies 
                        $.cookie('length',e.data.cartGoodsNum,{path:'/'}); 
                        if(e.code=='0'){ 
                            cook(); 
                            tc(e.message); 
                            var url=window.location.href; 
                            var size=url.split('=')[1]; 
                            if(size==='1'){ 
                                setTimeout(function(){ 
                                    window.location.href='/index.html'; 
                                    return false; 
                                },1100); 
                            } 
                            if(size==='2'){ 
                                setTimeout(function(){ 
                                    window.history.go(-1); 
                                    return false; 
                                },1100); 
                            } 
                            if(size===undefined){ 
                                setTimeout(function(){ 
                                    window.location.href='/index.html'; 
                                    return false; 
                                },1100); 
                            } 
                            if(size!='1'&&size!='2'&&size!=undefined){ 
                                setTimeout(function(){ 
                                    window.location.reload(); 
                                    return false; 
                                },1100); 
                            } 
                        }else{ 
                            tc(e.message); 
                        } 
                    } 
                }); 
                return false; 
            } 
        }); 
    }
    ajax_form(); 
    // 记录密码自动登录
    var username=$.cookie('username');
    var password=$.cookie('password');
    $('#phone').val(username);
    $('#password').val(password);
    if(username!=''&&username!=null&&password!=''&&password!=null){
        $('#checkbox_m').attr('checked',true);
    } 
});